<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sys_industry_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();
	}


	public function index(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();

		$this->load->library("common/right_manage");
		$data['update_industry']	=false;
		if ($this->right_manage->check_right("update_industry")) {
			$data['update_industry']	=true;
		}
		$data['delete_industry']	=false;
		if ($this->right_manage->check_right("delete_industry")) {
			$data['delete_industry']=true;
		}
		$data['add_industry']	=false;
		if ($this->right_manage->check_right("add_industry")) {
			$data['add_industry']	=true;
		}
		$data['query_industry']	=false;
		if ($this->right_manage->check_right("query_industry")) {
			$data['query_industry']	=true;
		}



		$this->load->library("sys/industry");
		$industry_id			= intval($this->input->get('industry_id'));
		$data['top_industrys']	= $this->industry->get_industrys(0);

		$data['industrys']= null;
		$data['top_industry_id']  =0;
		$data['second_industrys']  =null;
		$data['second_industry_id']  =0;
		$data['has_link']=true;

		if ($industry_id==null) {
			$data['top_industrys']		= $this->industry->get_industrys(0);
			$data['top_industry_id']	=0;
			$data['industrys']			=$data['top_industrys'];
		}else{
			$data['top_industry_id']	=$industry_id;
			$data['second_industrys'] 	=$this->industry->get_industrys($industry_id);
			$data['industrys']			=$data['second_industrys'];
			$data['has_link']=false;
		}
		$data['industry_id']=$industry_id;
		$this->load->view("sys/industry/industry_main",$data);

	}

	public function add_industry(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_industry",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/industry");
		$data['top_industrys']	= $this->industry->get_industrys(0);


		$this->load->view("sys/industry/add_industry",$data);

	}

	public function save_industry(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_industry",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$industry['industry_name'] =trim($this->input->get_post("industry_name"));
		$industry['parent_id'] =$this->input->get_post("parent1");

		$this->load->library("sys/industry");
		$this->load->helper("common");
		$result =$this->industry->save_industry($industry);
		echo JSON($result);
	}

	public function get_industrys(){
		$this->load->helper("common");
		$this->load->library("sys/industry");
		$industry_id=$this->input->get('industry_id');
		if ($industry_id==null) {
			$industry_id=0;
		}
		$industrys=$this->industry->get_industrys($industry_id);

		if ($industrys=="") {
			$industrys=array(array("industry_id"=>$industry_id, "industry_name"=>" - "));
		}
		echo JSON($industrys);
	}

	public function delete_industry(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_industry",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/industry");
		$industry_id	= intval($this->input->get('id'));
		$this->industry->set_delete_industry($industry_id);
		echo "<script>history.go(-1)</script>";
	}

	public function edit_industry(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();


		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_industry",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$industry_id =$this->input->get('id');

		$this->load->library("sys/industry");
		$data['top_industrys']	= $this->industry->get_industrys(0);
		$data['top_industry_id']  =0;
		$industry_info= $this->industry->get_industry_info($industry_id);

		if ($industry_info['parent_id']==0) {
			$data['top_industry_id']	=0;
		}else{
			$data['top_industry_id'] 		=$industry_info['parent_id'];
			$data['second_industrys']  		=$this->industry->get_industrys($industry_info['parent_id']);
			$data['second_industry_id']  	=0;
			//第三级 区
		}
		$data['industry_info']=$industry_info;
		$this->load->view("sys/industry/edit_industry",$data);
	}

	public function update_industry(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}

		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_industry",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$industry['industry_name'] =trim($this->input->get_post("industry_name"));
		$industry['parent_id']=$this->input->get_post("parent1");
		$industry_id =$this->input->get_post('industry_id');
		$this->load->library("sys/industry");
		$this->load->helper("common");
		$result =$this->industry->update_industry($industry,$industry_id);
		echo JSON($result);
	}

	public function get_api_industrys(){
		$this->load->helper("common");
		$this->load->library("sys/industry");
		$industry_id=$this->input->get('industry_id');
		if ($industry_id==null) {
			$industry_id=0;
		}

		$result['ret_code']=0;
		$result['err_code']=0;
		$industrys=$this->industry->get_industrys($industry_id);
		$result['data']=$industrys;

		echo JSON($result);
	}

}